from enum import Enum, unique
from typing import Optional


@unique
class APIStatusCode(Enum):
    """接口响应状态码枚举类
    """
    SUCCESS = {0: "Success"}
    PERMISSION_DENIED_CODE = {401: "You do not have the authority to access this page!"}
    ALREAD_EXISTED_CODE = {403: "The data already exists"}
    NOT_FOUND_CODE = {404: "The data you specified has been deleted or does not exist !"}
    SERVER_ERROR_CODE = {500: "An error occurred inside the server"}

    FILE_OR_DIR_DEAL_ERROR = {10001:"文件或目录处理错误"}
    FILE_OR_DIR_DEAL_EXTION = {10002: "文件或目录处理错误"}


    @property
    def code(self):
        """根据枚举值取状态码 -> `code` """
        # self is the member here
        return list(self.value.keys())[0]

    @property
    def message(self):
        """根据枚举值取状态码说明 -> `message` """
        return list(self.value.values())[0]

    @classmethod
    def message_by_code(cls, code) -> Optional[str]:
        """根据状态码获取状态码说明 """
        for name, member in cls.__members__.items():
            if code in member.value.keys():
                message = cls[name].message
                return message
        return None

    def __repr__(self):
        return "<%s.%s, %s>" % (self.__class__.__name__, self.name, self.value)

    def __str__(self):
        return "%s.%s" % (self.__class__.__name__, self.name)


